import { cloneDeep, } from 'lodash-es';

let onlineList: OnlineRecord[] = [
  {
    "tokenId": "8cd65099-c7ff-4954-b6f8-a2dadcae8edc",
    "deptName": "研发部门",
    "userName": "admin",
    "ipaddr": "127.0.0.1",
    "loginLocation": "内网IP",
    "browser": "Chrome 12",
    "os": "Windows 10",
    "loginTime": 1720599255838
  },
  {
    "tokenId": "54779a99-bf85-4e6d-889d-d41ad1d8a813",
    "deptName": "研发部门",
    "userName": "admin",
    "ipaddr": "127.0.0.1",
    "loginLocation": "内网IP",
    "browser": "Chrome 12",
    "os": "Windows 10",
    "loginTime": 1720596503116
  },
  {
    "tokenId": "083a78f7-8a26-4d69-a0ed-94f9f9b4b630",
    "deptName": "研发部门",
    "userName": "admin",
    "ipaddr": "127.0.0.1",
    "loginLocation": "内网IP",
    "browser": "Chrome 12",
    "os": "Windows 10",
    "loginTime": 1720598662625
  },
  {
    "tokenId": "9e06ee06-9ef7-42a7-ab89-58a05fdab22a",
    "deptName": "研发部门",
    "userName": "admin",
    "ipaddr": "127.0.0.1",
    "loginLocation": "内网IP",
    "browser": "Chrome 12",
    "os": "Windows 10",
    "loginTime": 1720626736079
  }
];

const mocks: MockRecord[] = [
  {
    method: "get",
    url: "/api/monitor/online/list",
    response(option) {
      const copyList = cloneDeep(onlineList);

      let resList = onlineList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: onlineList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/monitor/online/:onlineId",
    response(option) {
      const resOnline = onlineList.find(item => item.onlineId == option.query.onlineId);

      const resData: responseType = {
        code: resOnline ? 200 : 500,
        msg: resOnline ? "查询成功" : "查询失败",
        data: resOnline,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/monitor/online",
    response(option) {
      const newOnline = option.body;

      newOnline.onlineId = onlineList.length + 1;
      onlineList.unshift(newOnline);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/monitor/online",
    response(option) {
      const newOnline = option.body;

      const index = onlineList.findIndex(item => item.onlineId == newOnline.onlineId);

      onlineList.splice(index, 1, newOnline);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/monitor/online/:onlineIds",
    response(option) {
      const onlineIds = option.query.onlineIds.split(',');

      const filterList = onlineList.filter(item => !onlineIds.includes(String(item.onlineId)));

      onlineList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  }
];

export default mocks;